package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.StandardMaterialReplaceService;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

@Controller
@RequestMapping("/StandardMaterialReplace/*")
public class OperationStandardMaterialReplaceController extends BaseController { 
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	StandardMaterialReplaceService standardMaterialReplaceService;

	/**
	 * 从请求中获取参数
	 * 
	 * @return
	 */
	private Map<String, String> getParams() {
		Map<String, String> map = new HashMap<String, String>();
		String task_code = RequestUnit.getParameter("TASK_ID");
		if (StringUtil.IsNotNullOrEmpty(task_code)) {
			map.put("task_code", task_code);
		}
		String start = RequestUnit.getParameter("START");
		if (StringUtil.IsNotNullOrEmpty(start)) {
			map.put("start", start);
		}
		String end = RequestUnit.getParameter("END");
		if (StringUtil.IsNotNullOrEmpty(end)) {
			map.put("end", end);
		}
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		if (StringUtil.IsNotNullOrEmpty(wwtp_id)) {
			map.put("wwtp_id", wwtp_id);
		}
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		if (StringUtil.IsNotNullOrEmpty(flow_type)) {
			map.put("flow_type", flow_type);
		}
		String station_id = RequestUnit.getParameter("STATION_ID");
		if (StringUtil.IsNotNullOrEmpty(station_id)) {
			map.put("station_id", station_id);
		}
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			map.put("water_type", water_type);
		}
		String station_name = RequestUnit.getParameter("STATION_NAME");
		if (StringUtil.IsNotNullOrEmpty(station_name)) {
			map.put("station_name", station_name);
		}
		return map;
	}

	/**
	 * 获取标准物质更换记录
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStandardMaterialReplaceList")
	public void getStandardMaterialReplaceList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		PageInfo pageInfo = DataUtil.getPageInfo();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> list = standardMaterialReplaceService.getStandardMaterialReplaceList(getParams(), pTable, pageInfo);
		
		for (int i = 0;i<list.size();i++) {
			RowBase row = new RowBase();
			row = list.get(i);
			Date makeDate = (Date) row.getValue("MAKE_TIME");
			Date replaceDate = (Date) row.getValue("REPLACE_TIME");
			list.get(i).put("MAKE_DATE", makeDate);
			list.get(i).put("REPLACE_DATE", replaceDate);
		}
		String result = DataUtil.listToJSONObject(list,null,pageInfo).toString();
		outString(result);
	}

	/**
	 * 导出标准物质更换记录表
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportDoc")
	public void exportDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> params = getParams();
		Map<String, Object> root = new HashMap<String, Object>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		//查询标准物质更换列表
		List<RowBase> replaceList = standardMaterialReplaceService.getStandardMaterialReplaceList(params, pTable,
				DataUtil.getPageInfo());
		//统计更换了哪些仪器名称，型号
		List<RowBase> materialList = standardMaterialReplaceService.getReplaceInstrutStatisticsList(params, pTable);
		
		for (int i = 0;i<replaceList.size();i++) {
			RowBase row = new RowBase();
			row = replaceList.get(i);
			Date makeDate = (Date) row.getValue("MAKE_TIME");
			Date replaceDate = (Date) row.getValue("REPLACE_TIME");
			row.put("MAKE_TIME", makeDate);
			row.put("REPLACE_TIME", replaceDate);
		}
		
		if(replaceList!=null && replaceList.size()>0) {
			RowBase base = new RowBase();
			base=replaceList.get(0);
			root.put("USERNAME",base.getSafeString("USERNAME"));
			root.put("STATION_NAME",base.getSafeString("STATION_NAME"));
		}
		for(int i=0;i<materialList.size();i++) {
			int replaceNumber=0;
			RowBase row = new RowBase();
			List<RowBase> data = new ArrayList<RowBase>();
			row = materialList.get(i);
			String param = row.getSafeString("PARAM");
			for(int j=0;j<replaceList.size();j++) {
				RowBase rowS = new RowBase();
				rowS= replaceList.get(j);
				String paramS = rowS.getSafeString("PARAM");
				if(param.equals(paramS)) {
					replaceNumber++;
					data.add(rowS);
				}
			}
			materialList.get(i).put("replaceItem",data);
			materialList.get(i).put("replaceNumber",replaceNumber-1<0?0:replaceNumber-1);
			
		}
		root.put("materialList", materialList);
		root.put("materialNumber", (int)Math.ceil((double)materialList.size()/2)==0?0:((int)Math.ceil((double)materialList.size()/2))-1);
		outWord(root, "sw/standardMaterialReplace.xml", "标准物质更换记录表.doc");
	}

	/**
	 * 获取字典项
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDataDomain")
	public void getJobList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String strDomainName = RequestUnit.getString("DomainName");
		String strSQL = " select * from DATA_DEFULATDOMAIN t where t.S_DOMAINNAME='" + strDomainName + "' ";
		String result = DataUtil.getPageSelectJson(strSQL, null);
		outString(result);
	}

}
